package com.huobao.yunying.activitys;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.xutils.common.util.LogUtil;
import org.xutils.view.annotation.ViewInject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.entity.My_address;
import com.huobao.yunying.entity.Regions;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.utils.SelectAreaPopupWindow;
import com.huobao.zhangying.R;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Act_Edit_Address extends HuobaoBaseActivity implements OnClickListener {
	// 关闭按钮
	@ViewInject(R.id.tv_close)
	private TextView tv_close;
	// 收件人
	@ViewInject(R.id.edt_name)
	private EditText edt_name;
	// 手机号码
	@ViewInject(R.id.edt_shoujihao)
	private EditText edt_shoujihao;
	// 电话
	@ViewInject(R.id.edt_dianhua)
	private EditText edt_dianhua;
	// 地址
	@ViewInject(R.id.edt_address)
	private EditText edt_address;
	// 邮编
	@ViewInject(R.id.edt_youbian)
	private EditText edt_youbian;

	private TextView tv_address;
	@ViewInject(R.id.llyt_true_address)
	private View llyt_true_address;

	private List<Regions> m_AllRegions = new ArrayList<Regions>();

	private String region_name, region_id;


	private My_address address;

	@Override
	protected int getContentView() {
		return R.layout.layout_edit_address;
	}

	@Override
	protected String getTitleText() {
		return null;
	}

	@Override
	protected void init() {
		tv_address = (TextView) findViewById(R.id.tv_address);
		mAbTitleBar.setVisibility(View.GONE);
		address = (My_address) getIntent().getSerializableExtra("data");
		if (address != null) {
			this.region_id = address.region_id;
			this.region_name = address.region_name;
		}
		llyt_true_address.setOnClickListener(this);
		tv_close.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}

		});
		if (address != null)
			showDefault();
		getRegions();
	}

	private void showDefault() {
		edt_name.setText(address.consignee);
		edt_shoujihao.setText(address.phone_mob);
		edt_dianhua.setText(address.phone_tel);
		edt_address.setText(address.address);
		edt_youbian.setText(address.zipcode);
		tv_address.setText(address.region_name);
	}

	public void onSubmitClick(View v) {

		if(region_id == null || region_name == null){
			Toast.makeText(context, "请选择地址", Toast.LENGTH_SHORT).show();
			return;
		}
		String consignee = edt_name.getText().toString().trim();
		String phone_mob = edt_shoujihao.getText().toString().trim();
		String phone_tel = edt_dianhua.getText().toString().trim();
		String straddress = edt_address.getText().toString().trim();
		String zipcode = edt_youbian.getText().toString().trim();
		Map<String, String> map;
		if (address == null) {
			map = HttpUtil.getParams("app", "order", "act", "saveaddress", "consignee", consignee, "phone_mob",
					phone_mob, "phone_tel", phone_tel, "address", straddress, "zipcode", zipcode, "region_id",
					region_id, "region_name", region_name);
		} else {
			map = HttpUtil.getParams("app", "member", "act", "addressedit", "addr_id", address.addr_id, "consignee",
					consignee, "phone_mob", phone_mob, "phone_tel", phone_tel, "address", straddress, "zipcode",
					zipcode, "region_id", region_id, "region_name", region_name);
		}
		Http.getInstant().sendRequest(context, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				LogUtil.e(result);
				String str;
				if (address == null)
					str = "添加成功";
				else
					str = "修改成功";
				Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
				setResult(RESULT_OK);
				finish();
			}

			@Override
			public void onHttpError(String error) {
				// TODO Auto-generated method stub

			}
		});
	}

	@Override
	public void onClick(View v) {
		final SelectAreaPopupWindow popupWindow = new SelectAreaPopupWindow();
		switch (v.getId()) {
		case R.id.tv_guojia:
			break;
		case R.id.llyt_true_address:
			Intent intent = new Intent(this, Act_SelectArea.class);
			startActivityForResult(intent, 1);
			// 编辑编辑地址
			break;
		}
	}
	
	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		// TODO Auto-generated method stub
		if(arg0 == 1 && arg1 == RESULT_OK){
			region_id = arg2.getStringExtra("id");
			region_name = "中国 " + arg2.getStringExtra("name");
			tv_address.setText(region_name);
		}
		super.onActivityResult(arg0, arg1, arg2);
	}

	private void getRegions() {
		Map<String, String> map = HttpUtil.getParams("app", "regions");
		Http.getInstant().sendRequest(context, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				JSONArray array = jsonObject.getJSONArray("retval");
				for (int i = 0; i < array.size(); i++) {
					JSONObject object = array.getJSONObject(i);
					Regions regions = new Regions();
					regions.region_id = object.getString("region_id");
					regions.region_name = object.getString("region_name");
					regions.children = JSON.parseArray(object.getString("children"), Regions.class);
					m_AllRegions.add(regions);
				}
			}

			@Override
			public void onHttpError(String error) {
				// TODO Auto-generated method stub

			}
		});
	}

}
